package com.gitee.base.modules.core.controller;

import com.gitee.base.modules.BaseController;
import com.gitee.base.modules.core.model.SysRole;
import com.gitee.base.modules.core.model.SysUser;
import com.gitee.base.modules.core.service.ISysRoleService;
import com.gitee.base.modules.core.service.ISysUserService;
import com.gitee.base.security.permission.annotation.RequiresPermissionsDesc;
import com.gitee.base.utils.JsonVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@Controller
@RequestMapping("/core/user")
@Validated
public class CoreUserController extends BaseController {

    @Autowired
    private ISysUserService sysUserService;
    @Autowired
    private ISysRoleService sysRoleService;


    @RequiresPermissionsDesc(folder = "系统管理", menu = "用户管理", button = "查看", page = true)
    @RequiresPermissions("core:user:list")
    @GetMapping("/list")
    public String list(ModelMap modelMap) {
        List<SysUser> sysUsers = this.sysUserService.list(new LambdaQueryWrapper<SysUser>().orderByDesc(SysUser::getCtime));
        if (sysUsers != null && !sysUsers.isEmpty()) {
            List<SysRole> sysRoles = this.sysRoleService.list();
            for (SysUser sysUser : sysUsers) {
                List<Integer> userRoleIds = Arrays.stream(sysUser.getRoleIds().split(",")).map(Integer::valueOf).collect(Collectors.toList());
                sysUser.setRoleNames(sysRoles.stream().filter(r -> userRoleIds.contains(r.getId())).map(SysRole::getName).collect(Collectors.joining(",")));
            }
        }
        modelMap.put("userList", sysUsers);
        return "core/user/list";
    }

    @RequiresPermissionsDesc(folder = "系统管理", menu = "用户管理", button = "添加")
    @RequiresPermissions("core:user:add")
    @GetMapping("/add")
    public String add(ModelMap modelMap) {
        SysUser sysUser = getLoginUser();
        if(sysUser!=null){
            List<Integer> roleIdList = Stream.of(sysUser.getRoleIds().split(",")).map(Integer::valueOf).collect(Collectors.toList());
            if(roleIdList.contains(1)){
                modelMap.put("roleList",this.sysRoleService.list());
            }else{
                modelMap.put("roleList",this.sysRoleService.listByIds(roleIdList));
            }
        }
        return "core/user/addOrEdit";
    }

    @RequiresPermissions("core:user:add")
    @PostMapping("/save")
    @ResponseBody
    public JsonVo save(@Validated SysUser sysUser) {
        sysUser.setPassword(DigestUtils.md5Hex("123456"));
        this.sysUserService.save(sysUser);
        return JsonVo.success();
    }

    @RequiresPermissionsDesc(folder = "系统管理", menu = "用户管理", button = "修改")
    @RequiresPermissions("core:user:edit")
    @GetMapping("/edit")
    public String edit(ModelMap modelMap, @NotNull Integer id) {
        SysUser sysUser = this.sysUserService.getById(id);
        if(sysUser!=null){
            List<Integer> roleIdList = Stream.of(sysUser.getRoleIds().split(",")).map(Integer::valueOf).collect(Collectors.toList());
            if(roleIdList.contains(1)){
                modelMap.put("roleList",this.sysRoleService.list());
            }else{
                modelMap.put("roleList",this.sysRoleService.listByIds(roleIdList));
            }
            modelMap.put("user", sysUser);
        }
        return "core/user/addOrEdit";
    }

    @RequiresPermissions("core:user:edit")
    @PostMapping("/update")
    @ResponseBody
    public JsonVo update(@Validated SysUser sysUser) {
        this.sysUserService.updateById(sysUser);
        return JsonVo.success();
    }

    @RequiresPermissionsDesc(folder = "系统管理", menu = "用户管理", button = "锁定/解锁")
    @RequiresPermissions("core:user:lock")
    @PostMapping("/lock")
    @ResponseBody
    public JsonVo lock(@NotNull @Min(value = 2,message = "超管不能锁定/解锁") Integer id,@NotNull @Min(value = 0,message = "非法参数") @Max(value = 1,message = "非法参数") Short status){
        this.sysUserService.updateById(SysUser.builder().id(id).status(status).build());
        return JsonVo.success();
    }

    @RequiresPermissionsDesc(folder = "系统管理", menu = "用户管理", button = "删除")
    @RequiresPermissions("core:user:delete")
    @PostMapping("/delete")
    @ResponseBody
    public JsonVo delete(@NotNull @Min(value = 2, message = "超管不能删除") Integer id) {
        this.sysUserService.updateById(SysUser.builder().id(id).deleted(true).build());
        return JsonVo.success();
    }

}
